from django.shortcuts import render
from rest_framework.mixins import RetrieveModelMixin, CreateModelMixin
from rest_framework.viewsets import GenericViewSet

from comment.serializer import ProjectAllCommentViewSerializer, ProjectCommentCreateSerializer
# Create your views here.

from home import models
from utils.new_common_response import common_data
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated
from rest_framework.viewsets import GenericViewSet
from rest_framework_jwt.authentication import JSONWebTokenAuthentication


class ProjectAllCommentView(RetrieveModelMixin, GenericViewSet):
    """
    ymq
    详情页评论
    """
    queryset = models.Project.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = ProjectAllCommentViewSerializer

    def retrieve(self, request, *args, **kwargs):
        response = super().retrieve(request, *args, **kwargs)
        old_data = response.data
        new_data = common_data(old_data)
        response.data = new_data

        return response


class ProjectCommentCreateView(CreateModelMixin, GenericViewSet):
    """
    创建评论视图
    """
    serializer_class = ProjectCommentCreateSerializer
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def create(self, request, *args, **kwargs):
        response = super().create(request, *args, **kwargs)
        old_data = response.data
        # print(old_data, '>>>>>>>>>>>>')

        new_data = common_data(old_data)
        response.data = new_data

        return response
